﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace TripleJump {
    /// <summary>
    /// 圆形地形
    /// </summary>
    public class CCircle {
        public CPara para = new CPara();

        public CCircle() {
        }

        /// <summary>
        /// 从世界方向到圆方向
        /// </summary>
        public Vector3 ToCircleDirection(Vector3 pos, Vector3 direction) {
            var up0 = pos - para.centerPoint;
            var quaternion = Quaternion.FromToRotation(Vector3.up, up0);
            return quaternion * direction;
        }

        /// <summary>
        /// 从圆方向到世界方向
        /// </summary>
        public Vector3 ToWorldDirection(Vector3 pos, Vector3 direction) {
            var direction0 = pos - para.centerPoint;
            var quaternion = Quaternion.FromToRotation(direction0, Vector3.up);
            return quaternion * direction;
        }

        /// <summary>
        /// 获取圆表面点
        /// </summary>
        public Vector3 ToCircleSurfacePos(Vector3 pos, Vector3 direction) {
            float radius = Vector3.Distance(para.centerPoint, pos);
            direction = new Vector3(0, direction.y, direction.z);
            //正弦--获取角度
            float b = (direction / 2).magnitude;
            if (radius <= b) {
                Debug.LogErrorFormat("错误___direction长度过大，无法在球形表面 ");
                return Vector3.zero;
            }
            float zSin = b / radius;
            float z弧度 = Mathf.Asin(zSin);
            float angle = z弧度 * 180 / Mathf.PI;
            Quaternion addAngle = Quaternion.Euler(angle * 2, 0, 0);
            Vector3 vv = addAngle * (pos - para.centerPoint);
            return vv + para.centerPoint;
        }

        /// <summary>
        /// 到圆表面的旋转
        /// </summary>
        public Quaternion ToCircleRotation(Vector3 rolePos) {
            Vector3 downDirection = para.centerPoint - rolePos;
            return Quaternion.FromToRotation(Vector3.up, -downDirection);
        }

        public class CPara {
            /// <summary>
            /// 中心点
            /// </summary>
            public Vector3 centerPoint;
            /// <summary>
            /// 是否球形
            /// </summary>
            public bool isSphere = true;
        }
    }
}
